package com.yang.yangCommand;

import com.yang.YangCuiLian;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;

import java.util.ArrayList;
import java.util.List;

public class YangGUICommand implements CommandExecutor, Listener {
    private YangCuiLian yangCuiLian;

    public YangGUICommand(YangCuiLian yangCuiLian) {
        this.yangCuiLian = yangCuiLian;
    }


    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        //执行指令
        if (!label.equalsIgnoreCase("cuiLianGUI")) {
            return true;
        }
//        if (args.length == 0){
//            return true;
//        }
        // 判断是否是玩家执行
        if (!(sender instanceof Player)) {
            return true;
        }
        Player player = (Player) sender;
        openMainMenu(player);
        return true;
    }

    // 创建菜单方法
    public void openMainMenu(Player player) {
        Inventory menu = Bukkit.createInventory(null, 9, "淬炼菜单");
        ItemStack pane = new ItemStack(Material.STAINED_GLASS_PANE);
        // 设置边框为玻璃
        menu.setItem(1, pane);
        menu.setItem(2, pane);
        menu.setItem(3, pane);
        ItemStack anvilStack = new ItemStack(Material.ANVIL);
        ItemMeta anvilMeta = anvilStack.getItemMeta();
        List<String> anvilLoreList = new ArrayList<>();
        anvilLoreList.add(ChatColor.YELLOW + "左边放装备");
        anvilLoreList.add(ChatColor.YELLOW + "右边放淬炼石");
        anvilMeta.setLore(anvilLoreList);
        anvilMeta.setDisplayName(ChatColor.RED + "开始淬炼");
        anvilStack.setItemMeta(anvilMeta);
        menu.setItem(4, anvilStack);
        menu.setItem(5, pane);
        menu.setItem(6, pane);
        menu.setItem(7, pane);
//        for (int i = 0; i < 9; i++) {
//            menu.setItem(i, pane);
//            menu.setItem(i + 45, pane);
//        }
//
//        for (int i = 9; i < 45; i += 9) {
//            menu.setItem(i, pane);
//            menu.setItem(i + 8, pane);
//        }
        // 添加按钮
//        ItemStack playerHead = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
//         获取SkullMeta
//        SkullMeta skullMeta = (SkullMeta) playerHead.getItemMeta();
//        List<String> loreList = new ArrayList<>();
//        loreList.add("玩家信息:" + player.getName());
//        loreList.add(ChatColor.YELLOW + "游戏金币：" + 100.00);
//        loreList.add("称号：超级管理员");
//        skullMeta.setLore(loreList);
//        playerHead.setItemMeta(skullMeta);
//        menu.setItem(0, playerHead);
//        ItemStack button2 = new ItemStack(Material.GOLDEN_APPLE);
//        menu.setItem(8, button2);
        // 打开菜单
        player.openInventory(menu);
    }

    // 处理菜单点击事件
    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        if (event.getInventory().getTitle().equals("淬炼菜单")) {

            Player player = (Player) event.getWhoClicked();
            ItemStack clickedItem = event.getCurrentItem();

            if (clickedItem != null) {
                if (clickedItem.getType() == Material.ANVIL) {
                    event.setCancelled(true); // 阻止玩家移动物品
                    player.sendMessage("开始淬炼！");

                    //获取GUI 的玻璃 点击淬炼时 灰色玻璃变成黄色玻璃
                    ItemStack pane = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 4);
                    for (int i = 0; i < 9; i++) {
                        if (i != 0 && i != 4 && i != 8) {
                            event.getInventory().setItem(i, pane);
                            //稍微慢点一秒变一个玻璃板
                        }
                    }


                    // 在这里执行与按钮相关的操作
                    // 获取第一格物品
                    ItemStack left = event.getInventory().getItem(0);
                    player.sendMessage(String.valueOf(left.getType()));
                    // 执行淬炼逻辑
                    performCuiLian(player, left);
                }
            }
        }
    }

    // 执行淬炼逻辑的方法
    private void performCuiLian(Player player, ItemStack leftItem) {
        // 在这里编写淬炼的逻辑
        // 可以根据具体需求修改和扩展
        // 示例：给物品添加一个 lore
        ItemMeta itemMeta = leftItem.getItemMeta();
        List<String> lore = itemMeta.getLore();
        if (lore == null) {
            lore = new ArrayList<>();
        }
        // 添加淬炼属性，这里以吸血为例
        double vampireChance = 0.1; // 吸血几率，可根据需求调整
        double vampireAmount = 0.1; // 吸血量，可根据需求调整

        lore.add(ChatColor.GREEN + "吸血 " + ChatColor.YELLOW + "几率: " + vampireChance * 100 + "%");
        lore.add(ChatColor.GREEN + " 量: " + vampireAmount * 100 + "%");
        lore.add(ChatColor.GREEN + "力量: " + 100);

        lore.add(ChatColor.GREEN + "已淬炼");
        itemMeta.setLore(lore);
        leftItem.setItemMeta(itemMeta);

        // 更新玩家物品
        player.getInventory().setItem(0, leftItem);

        // 可以在这里加入其他淬炼的效果和逻辑
    }
}
